package com.jzsqdy.mycat.thread;

import com.jzsqdy.mycat.common.PointDao;
import com.jzsqdy.mycat.domain.Statistic;

import java.util.Map;

/**
 * 使用mycat访问数据库
 * @author chenjf
 */
public class Query implements Runnable {

    private PointDao pointDao;
    private Statistic statistic;

    public Query(PointDao pointDao, Statistic statistic) {
        this.pointDao = pointDao;
        this.statistic = statistic;
    }

    public void run() {
        while (true){
            query();
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }


    }

    public void query() {

        long start = System.currentTimeMillis();
        for (int i=0;i<300;i++){
            Map<String,Object> map = pointDao.query(statistic.getMonitoredObjectId());
        }
        long end = System.currentTimeMillis();
        long time = (end - start)/300;

        statistic.setAvgTime(time);

        statistic.setData(pointDao.count());
        System.out.println(statistic);
    }

    public void query(String objId) {

        long start = System.currentTimeMillis();
        for (int i=0;i<300;i++){
            Map<String,Object> map = pointDao.query(objId);
        }
        long end = System.currentTimeMillis();
        long time = (end - start)/300;

        statistic.setAvgTime(time);

        statistic.setMonitoredObjectId(objId);
        statistic.setData(pointDao.count());
        System.out.println(statistic);
    }

}
